//
//  CRNearPersonController.m
//  CRChatRoom
//
//  Created by lanou3g on 15/11/29.
//  Copyright © 2015年 SecondGroup. All rights reserved.
// 附近的人

#import "CRNearPersonController.h"
#import "CRTool.h"
#import "FriendCell.h"
#import "Friend.h"
#import "MJRefresh.h"

@interface CRNearPersonController ()

/**
 *  存放附近的人信息的数组
 */
@property (nonatomic, strong) NSArray *friends;

@end

@implementation CRNearPersonController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIButton *button = [[UIButton alloc] init];
    button.bounds = CGRectMake(0, 0, 30, 30);
    [button setBackgroundImage:[UIImage imageNamed:@"backNormal"] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"baclhigh"] forState:UIControlStateHighlighted];
    [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    
    
    
    UIButton *rightButton = [[UIButton alloc] init];
    rightButton.bounds = CGRectMake(0, 0, 40, 30);
    [rightButton setBackgroundImage:[UIImage imageNamed:@"fujin_normal"] forState:UIControlStateNormal];
    [rightButton setBackgroundImage:[UIImage imageNamed:@"fujin_click"] forState:UIControlStateHighlighted];
    [rightButton addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    //    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    
    
    [self.tableView registerClass:[FriendCell class] forCellReuseIdentifier:@"friend"];
    
    // 添加下拉刷新控件
    [self setupRefresh];
    
    [self.tableView.mj_header beginRefreshing];
    
    
    
}

// 返回
- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}
// 一点按钮就刷新
- (void)click
{
    [self.tableView.mj_header beginRefreshing];
}

// 添加下拉刷新控件
- (void)setupRefresh
{
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh)];
    self.tableView.mj_header = header;
}



- (void)headerRefresh
{
    // 查找附近的人
    [CRTool findNearPersonWith:^(NSArray *data) {
        
        NSMutableArray *array = [NSMutableArray array];
        for (AVObject *obj in data) {
            NSDictionary *dict = obj[@"userinfo"];
            Friend *friend = [Friend friendWithDict:dict];
            [array addObject:friend];
        }
        
        self.friends = array;
        // 刷新tableVIew
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    }];
}


- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    // 保存用户地理位置到服务器
    [CRTool saveUserLocation];
    
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.friends.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    FriendCell *cell = [FriendCell cellWithTableView:tableView];
    
    cell.friendData = self.friends[indexPath.row];
    
    return cell;
}



@end






